clear all

tic
% mainFolder=['C:\Users\Tom�s\Documents\1. Regehr Lab\Data\MLI-PC_PairedRecs\'];
mainFolder=['C:\Users\Tom�s\Desktop\Candelabrum Cell paper\Candelabrum Cells\CC_to_PC_INDIRECT\'];
formatpattern = fullfile(mainFolder, '**/*.ibw');
dinfo = dir(formatpattern);

%% First thing to do is to figure out how many pairs I have and to create variables that contain the folders that contain each pair
pairCount=0;
folderNames={};

for o=1:numel(dinfo);
    if sum(strcmp(dinfo(o).folder,folderNames(:)))==0
        
               pairCount=pairCount+1;
        folderNames(pairCount)={[dinfo(o).folder]};
    end
end

% use char to convert the cells in folderNames into strings to use as
% folder variable to run the other script. e.g. char(folderNames(1))

keepvars = {'pairCount','folderNames'};
clearvars('-except', keepvars{:});

%% now run the CC_Train_Paired (batch version) analysis on these and extract the binned firing rate and binnedIPSCcharge per pair
IPSC_Charge=[];
preFR=[];

for pair=1:pairCount
    dir_to_search=[];
    dir_to_search=char(folderNames(pair));
    
    CC_Train_Paired_Batch  %run the script to analyze each folder with a pair.
    IPSC_Charge(:,pair)=meanChargeBinned;
    preFR(:,pair)=FR_Temp;
    keepvars = {'preFR','IPSC_Charge','folderNames','pairCount','pair','time3'};
    clearvars('-except', keepvars{:});
    close gcf
end

meanIPSCCharge=mean(IPSC_Charge,2);
    

% errorbar(x,y,err)

%% plotting shit

figure;
hold on
for i=1:pairCount
   
plot(time3,IPSC_Charge(:,i),'color',[0 0 0 1/4])
end
plot(time3,meanIPSCCharge,'color',rgb('firebrick'),'linewidth',2)
Oylim=ylim;
ylim([0 Oylim(2)])
set(gca,'FontSize', 18);
set(gcf,'color','white');
box off
xlabel('time (s)')
ylabel('IPSC Charge (A.U)')
vline([5 10],'--k');
%%
% create a normalized version to better compare across cells
for i=1:pairCount
    tempPair=IPSC_Charge(:,i);
    normIPSCCharge(:,i)=tempPair./mean(tempPair(1:10));
end

figure;
hold on
for i=1:pairCount
   
plot(time3,normIPSCCharge(:,i),'color',[0 0 0 1/4])
end
plot(time3,mean(normIPSCCharge,2),'color',rgb('firebrick'),'linewidth',2)
Oylim=ylim;
ylim([0 Oylim(2)])
set(gca,'FontSize', 18);
set(gcf,'color','white');
box off
xlabel('time (s)')
ylabel('IPSC Charge (A.U)')
vline([5 10],'--k');

%% now the presynaptic FR

time4=15/74:15/74:15;

figure;
hold on
for i=1:pairCount
   
plot(time4,preFR(:,i),'color',[0 0 0 1/4])
end
plot(time4,mean(preFR,2),'color',rgb('deepskyblue'),'linewidth',2)
Oylim=ylim;
ylim([0 Oylim(2)])
set(gca,'FontSize', 18);
set(gcf,'color','white');
box off
xlabel('time (s)')
ylabel('CC firing rate (sp/s)')
vline([5 10],'--k');




% summarize before during after

before=mean(normIPSCCharge([1:10],:),1);
during=mean(normIPSCCharge([11:20],:),1);
after=mean(normIPSCCharge([21:30],:),1);

figure
plot((ones([15,3]).*[1 2 3]),[before' during' after'],'ko')
hold on
plot((ones([15,3]).*[1 2 3])',[before' during' after']','color',[0 0 0 0.2],'linewidth',1)

meanValues=mean([before' during' after'],1);
%plot means on top
plot([1:3],meanValues,'color','k','linewidth',2);
scatter([1:3],meanValues,'markerFaceColor','k','markeredgeColor','k');
ylim([0 1.5])
ylabel('IPSC charge (norm)' )
xticks([1 2 3])
xticklabels({'baseline','CC firing','after'})
box off
set(gca,'TickDir','out');

% mean firing rate during step CCs
meanFR=mean(preFR([25:49],:),1);

figure;
plot(meanFR,1-during,'ro')

toc

    
 [h,p]=ttest(during,1,'alpha',0.05)
 [p,h]=signrank(during,1,'alpha',0.05) %wilcoxon signed rank test (single sample version of mann whitney)
 
 
 figure;
hold on

temp=during;

jitt=rand(numel(temp),1)/2;
% s4=scatter(1-1/6+jitt,temp,90'.','filled');
% s4.MarkerFaceColor=rgb('black');
% s4.MarkerFaceAlpha = 0.2;
s4=scatter(0-1/6+jitt,temp,100,'k','filled');
s4.MarkerFaceColor=[1 1 1];
% s4.MarkerFaceAlpha = alphaColors;
s4.MarkerEdgeColor=[0 0 0];


scatter(0, nanmean(temp(:)),100,'k','filled','markerFaceColor','k')
tempSEM=nanstd(temp(:))/sqrt(sum(~isnan(temp)))
plot([0 0],[(nanmean(temp(:)-tempSEM)) (nanmean(temp(:))+tempSEM)],'color',[0 0 0],'linewidth',2)

xlim([-1 1])
ylim([0 1.5])
set(gcf,'color','white');
set(gca,'FontSize', 12,'fontname','Arial');
box off
set(gca, 'YTick', [0:0.5:1.5],'tickdir','out')

xticks({})
ylabel('IPSC charge (norm)')
set(gcf,'Position',[1240 238 173 480]);


%make boxplot
figure
boxplot(temp, 'symbol','', 'color','k')
ylim([0 1.5])
ylabel('IPSC charge')
set(gcf,'Position',[1240 238 173 480]);
    
    
    
    
    